package com.dc.web.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ContentLoopMerge;
import com.dc.config.excel.EasyExcelUtil;
import com.dc.dao.EnergyCompanyDao;
import com.dc.dao.ShipAndRepairDao;
import com.dc.dao.ShipRepairDao;
import com.dc.dao.vo.ShipAndRepairVO;
import com.dc.entity.EnergyCompanyEntity;
import com.dc.entity.ShipEntity;
import com.dc.entity.ShipRepairEntity;
import com.dc.model.AjaxResult;
import com.dc.utils.DateUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    EnergyCompanyDao dao;

    @GetMapping("/list")
    public AjaxResult list() {
        List<EnergyCompanyEntity> all = dao.findAll();
        return AjaxResult.success(all);
    }

    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable("id") Long id) {
        Optional<EnergyCompanyEntity> byId = dao.findById(id);
        return AjaxResult.success(byId);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody EnergyCompanyEntity entity) {
        EnergyCompanyEntity save = dao.save(entity);
        return AjaxResult.success(save);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        dao.deleteById(id);
        return AjaxResult.success();
    }

    @PutMapping("/{id}")
    public AjaxResult update(@PathVariable("id") Long id, @RequestBody EnergyCompanyEntity entity) {
        entity.setId(id);
        dao.save(entity);
        return AjaxResult.success();
    }


    @Resource
    ShipRepairDao shipRepairDao;

    @RequestMapping("/tt")
    public AjaxResult tt() {
        PageRequest pageRequest = PageRequest.of(0  , 10);
        Page<ShipRepairEntity> byEnterTimeBetweenAndShipId =
                shipRepairDao.findByEnterTimeBetweenAndShipId(new Date(), new Date(), 123L, pageRequest);

        return AjaxResult.success();
    }

    @Resource
    private ShipAndRepairDao shipAndRepairDao;


    @RequestMapping("/tt2")
    public AjaxResult tt2() {
        Date enter = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", "2024-01-01 00:00:00");
        Date end = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", "2025-01-01 00:00:00");
        PageRequest request = PageRequest.of(0, 10);
        Page<ShipAndRepairVO> b = shipAndRepairDao.findShipAndRepairVOByEnterTimeBeginAndEnterTimeEndAndId(enter, end, 1L, request);
        return AjaxResult.success(b);
    }

    @GetMapping("/download4")
    public void download4(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");

            User user1 = new User();
            user1.setUserId(123);
            user1.setName("as");
            user1.setPhone("15213");
            user1.setEmail("5456");
            user1.setCreateTime(new Date());

            User user2 = new User();
            user2.setUserId(123);
            user2.setName("asbnm");
            user2.setPhone("15213");
            user2.setEmail("5456");
            user2.setCreateTime(new Date());

            User user3 = new User();
            user3.setUserId(123);
            user3.setName("as");
            user3.setPhone("46543213");
            user3.setEmail("5456");
            user3.setCreateTime(new Date());

            List<User> userList = Arrays.asList(user1, user2, user3);

//            EasyExcel.write(response.getOutputStream(), User.class)
//                    .registerWriteHandler(new EasyExcelUtil(0, new int[]{0, 2}))
//                    .sheet("模板")
//                    .doWrite(userList);
            EasyExcel.write(response.getOutputStream(), User.class)

                    .registerWriteHandler(new EasyExcelUtil(0, new int[]{0}))
                    .registerWriteHandler(new EasyExcelUtil(0, new int[]{2}))
                    .sheet("模板")
                    .doWrite(userList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class User {

        @ContentLoopMerge(eachRow = 2, columnExtend = 1)
        @ExcelProperty(value = "用户Id")
        private Integer userId;

        @ExcelProperty(value = "姓名")
        private String name;

        @ExcelProperty(value = "手机")
        private String phone;

        @ExcelProperty(value = "邮箱")
        private String email;

        @ExcelProperty(value = "创建时间")
        private Date createTime;
    }

}
